/**
 * Created by wrl on 2017/7/10.
 */
$(function(){
    $(document).keyup(function (e) {
        if(e.which==13){
            $('#selectBtnEnter').click();
        }
    })
});
var path = baseConfig.basePath;
var pagePath = '../privilege/selectPaging';
var updateId = '';
Vue.http.options.emulateJSON = true;
$(function(){
    var vue = new Vue({
        el:'#privilege-list',
        data:{
            pageInfo:{},
            searchData:{
                page:1,
                rows:10,
                functionName:null,
                functionType:'',
                isSearch:'0'
            },
            permissions:{},
            showPage:10     /*显示页码个数*/
        },
        mounted:function(){
            var self = this;

            self.$http.get(pagePath,{}).then(function(response){
                self.pageInfo = response.body.pagination;
                console.log(self.pageInfo)
                customUtils.paging('#pagination',self.pageInfo.pages,self.showPage,function(num) {
                    currentPage = num;
                    if(num == self.searchData.page)
                        return;
                    self.searchData.page = num;
                    self.$http.post(pagePath,self.searchData).then(function(response){
                        self.pageInfo = response.body.pagination;
                    })
                })
            },function(response){
                console.log('请求失败');
            });
          //遍历权限
    	  $.each(parent.globalPermissions, function(i,val) {
    		  self.$data.permissions[val.replace(':','_')]=true;
    	  });
        },
        filters: {
            functionTypeFilter: function(type) {
                switch (type){
                    case '1': return '菜单';
                    case '2': return '模块';
                    case '3': return '功能';
                }
            }
        },
        methods: {
            search: function () {       //查
                var self = this;
                self.searchData.page = 1;
                self.searchData.isSearch = '1';
                self.$http.post(pagePath,self.searchData).then(function(response){
                    self.pageInfo = response.body.pagination;
                    if(self.pageInfo.total == 0) return
                    customUtils.paging('#pagination',self.pageInfo.pages,self.showPage,function(num) {
                        currentPage = num;
                        if(num == self.searchData.page)
                            return;
                        self.searchData.page = num;
                        self.$http.post(pagePath,self.searchData).then(function(response){
                            self.pageInfo = response.body.pagination;
                        })
                    })
                },function(response){
                    console.log(response.body)
                })
            },
            updatePage:function(id){     //改
                layer_show('修改功能', 'admin-privilege-add-update.html?action=update&updateId='+id, '800','500');
            },
            delIDs:function(){
                var self = this;
                var idsList = [];
                $(':checked[class="cb-id"]').each(function(index,data){
                    idsList.push(this.value);
                    $(this).attr('name','ids');
                })
                if(idsList.length != 0){
                    layer.confirm('确认删除？',function(index){
                        var formData = $('#formForDel').serialize();
                        self.$http.post(path+'privilege/deleteByIds',formData).then(function(response){
                            if(response.body.flag){
                                tips('操作成功',6)
                                setTimeout(function(){
                                    location.replace(location.href);
                                },500)
                            }else{
                                tips('删除失败',5)
                            }
                        },function(response){
                            console.log(response.body)
                            tips('操作失败',5)
                        })
                    })
                }else{
                    tips('请选择需要删除的功能',5)
                }
            },
            del:function(id){        //删
                var self = this;
                layer.confirm('功能删除须谨慎，确认要删除吗？', function(index) {
                    var url = path+'privilege/deleteById';
                    self.$http.post(url,{'id':id}).then(function(response){
                        if(response.body.flag == true){
                            tips('已删除!',1)
                            setTimeout(function(){
                                location.replace(location.href);
                            },500);
                        }else{
                            tips('删除失败!',5)
                        }
                    },function(res){
                        console.log(res.body)
                    })
                })
            },
            addData:function(){         //增
                layer_show('权限', 'admin-privilege-add-update.html', '600','400');
            }
        }
    });
})